﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDbConnectionExtend;
using System.Data;
using System.Diagnostics;

namespace IDbConnetionExtendTest
{
    public class Program
    {
        public class MsgModel
        {
            public int Id { get; set; }
            public string PhoneNumber { get; set; }
            public string MsgDate { get; set; }
            public string MsgContent { get; set; }
            public string Name { get; set; }
        }

        public class system_collectinfoconfig
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string StartUrl { get; set; }
        }

        [Table("messageinfo")]
        public class MsgModel2
        {
            public int Id { get; set; }
            [Field("PhoneNumber")]//字段映射
            public string PhoneNum { get; set; }
            public string MsgDate { get; set; }
            public string MsgContent { get; set; }
        }
        [Table("userinfo")]
        public class User
        {
            public int Id { get; set; }
            [Field("UserName")]//字段映射
            public string uid { get; set; }
            [Field("Password")]//字段映射
            public string pwd { get; set; }
        }

        static void Main(string[] args)
        {

            //自动 open
            using (var con = new MySqlConnection("server=localhost;port=33060;uid=root;pwd=888888;database=crawler"))
            {
                var count1 = con.ExecuteDataSet<int>("select count(*) from system_collectinfoconfig", null, null, new MySqlDataAdapter(), ds => ds.Tables[0].Rows.Count);


                var count = con.ExecuteWithReader<string>("select count(*) from system_collectinfoconfig");
                var msgs = con.Query<system_collectinfoconfig>("select * from system_collectinfoconfig");
                var msgs1 = con.Query<MsgModel>("select * from system_collectinfoconfig ", new { PhoneNumber = "18377777777" });

                var list = new List<IDataParameter> { new MySqlParameter("@PhoneNumber", "18377777777") };
                var msgs11 = con.Query<MsgModel>("select * from messageinfo where PhoneNumber=@PhoneNumber", null, list);
                ////-------- 默认实现的
                var count2 = con.Count<MsgModel2>();
                var msgsinfo = con.Find<MsgModel2>(1);
                var msgs2 = con.GetAll<MsgModel2>();

            }
            //手动 open  
            using (var con = new MySqlConnection("server=localhost;port=3306;uid=root;pwd=admin;database=test"))
            {
                con.Open();

                var count1 = con.ExecuteDataSet<int>("select count(*) from messageinfo", null, null, new MySqlDataAdapter(), ds => ds.Tables[0].Rows.Count);
                var count = con.Execute<int>("select count(*) from messageinfo");
                var msgs = con.Query<MsgModel>("select * from messageinfo where PhoneNumber='18377777777'");
                var msgs1 = con.Query<MsgModel>("select * from messageinfo where PhoneNumber=@PhoneNumber", new { PhoneNumber = "18377777777" });

                var list = new List<IDataParameter> { new MySqlParameter("@PhoneNumber", "18377777777") };
                var msgs11 = con.Query<MsgModel>("select * from messageinfo where PhoneNumber=@PhoneNumber", null, list);
                ////-------- 默认实现的
                var count2 = con.Count<MsgModel2>();
                var msgsinfo = con.Find<MsgModel2>(1);
                var msgs2 = con.GetAll<MsgModel2>();

            }



            //工作单元测试
            using (var con = new MySqlConnection("server=localhost;port=3306;uid=root;pwd=admin;database=test"))
            {
                var unit = con.GetUnitWorker();
                var count = unit.DbConnection.Execute("delete from userinfo where id=4");
                count = unit.DbConnection.Add(new User { uid = DateTime.Now.ToString(), pwd = "123456" });
                unit.Commit();
                unit.Rollback();
            }

            {
                using (var con = new MySqlConnection("server=localhost;port=3306;uid=root;pwd=admin;database=test"))
                {
                    using (var unit = new UnitWorker(con))
                    {
                        var count = unit.DbConnection.Execute("delete from userinfo where id=4");
                        count = unit.DbConnection.Add(new User { uid = DateTime.Now.ToString(), pwd = "123456" });
                        unit.Commit();
                        unit.Rollback();
                    }
                }


            }

            Console.ReadLine();
        }
    }
}
